package Concurrency2.Chapter02_InnerLockPrinciple.P01_ProducerConsumer;

import java.util.ArrayList;
import java.util.List;

/**
 * 非线程安全的容器
 *
 */
public class DataBuffer<T> {
    //指定一个容器
    private List<T> container;
    private Integer AMOUNT = 10;

    public DataBuffer() {
        this.container = new ArrayList<>(AMOUNT);
    }

    public DataBuffer(Integer amount) {
        this.container = new ArrayList<>(amount);
    }

    // 功能1: 添加元素
    public void add(T data) {
        if (container.size() < AMOUNT) {
            this.container.add(data);
        } else {
            System.out.println("容器已满");
        }
    }

    // 功能2: 获取元素
    public T fetch() {
        if (container.size() > 0) {
            return this.container.remove(0);
        } else {
            System.out.println("容器已空");
            return null;
        }
    }
}
